 /**
 * 逻辑、功能相关描述:
 *
 * @author yandawei 编写日期：2008-10-16
 */
package com.ce.pms.common.aop.impl;

import org.apache.log4j.Logger;

import com.ce.pms.common.annotation.AssociatedBuildingQuery;
import com.ce.util.BlankUtil;
import com.ce.util.CurrentUserThread;

/**
 * @author yandawei
 *
 */
public aspect AssociateBuildingSQLQueryAdvice extends
		AbstractAssociateBuildingQueryAdvice {

	private Logger log =  Logger.getLogger(AssociateBuildingSQLQueryAdvice.class);

	pointcut sessionCreateSQLQuery(String sql): execution(* createSQLQuery(String,..)) && args(sql) ;

	Object around(String sql , AssociatedBuildingQuery associatedBuildingQuery): sessionCreateSQLQuery(sql) && cflowbelowAssociatedBuildingQuery(associatedBuildingQuery){
		log.info("===DACL===");
		Long userId = CurrentUserThread.get().getUserId();

		String alias = associatedBuildingQuery.alias();

		validateParameter("alias",alias);

		String beginStr = associatedBuildingQuery.beginStr();

		validateParameter("beginStr",beginStr);

		String endStr = associatedBuildingQuery.endStr();

		validateParameter("endStr",endStr);

		String insertAfterPoint = associatedBuildingQuery.insertAfterPoint();

		validateParameter("insertAfterPoint",insertAfterPoint);

		String property4Building = associatedBuildingQuery.property4Building();

		//第二匹配别名
		String aliasStr = associatedBuildingQuery.secondAlias();
		//第二匹配点
		String insertAfterPointStr = associatedBuildingQuery.secondInsertAfterPoint();

		boolean supportLoop = associatedBuildingQuery.supportLoop();

		boolean isFront = associatedBuildingQuery.isFront();

		//int startPos = sql.indexOf(insertAfterPoint);
		//if(startPos == -1){
		//	throw new DaclUnsupportedParameterException("没找到指定参数"+insertAfterPoint);
		//}
		//int tempLength = startPos+insertAfterPoint.length();
		//String begin = sql.substring(0,tempLength);
		//String end = sql.substring(tempLength);

		StringBuffer buffer = new StringBuffer(sql);
		int index = 0;

		if(!BlankUtil.isBlank(aliasStr) && !BlankUtil.isBlank(insertAfterPointStr)){
			String secondStr = null;
			if(isFront){//注意只对前台收费处使用
				secondStr = String.format(" %1$s ( %2$s.%3$s  IN  (SELECT ALIAS2.RESOUCE_ID FROM SEC_USER_R_RIGTHS ALIAS2 WHERE ALIAS2.TYPE_CODE = '002' AND ALIAS2.USER_ID = '%4$s') OR %2$s.%3$s  is NULL ) %5$s"
					,beginStr, aliasStr, property4Building, userId, endStr);
			}else{
				secondStr = String.format(" %1$s %2$s.%3$s IN (SELECT ALIAS.RESOUCE_ID FROM SEC_USER_R_RIGTHS  AS ALIAS WHERE ALIAS.TYPE_CODE ='002' AND ALIAS.USER_ID = '%4$s') %5$s"
						,beginStr, aliasStr, property4Building, userId, endStr);
			}
			index = buffer.indexOf(insertAfterPointStr,index+1);
			buffer.insert(index + insertAfterPointStr.length(),secondStr);
		}
		index = 0;
		String tmp = String
		.format(
				" %1$s %2$s.%3$s IN (SELECT ALIAS.RESOUCE_ID FROM SEC_USER_R_RIGTHS  AS ALIAS WHERE ALIAS.TYPE_CODE ='002' AND ALIAS.USER_ID = '%4$s') %5$s",
				beginStr, alias, property4Building, userId, endStr);

		do{
			index = buffer.indexOf(insertAfterPoint,index+1);
			if(index != -1)
				  buffer.insert(index + insertAfterPoint.length(),tmp);
		}while(index != -1 && supportLoop);

//		sqlStr = buffer.toString();
//		StringBuffer insertQuery = new StringBuffer();
//		insertQuery.append(beginStr)
//			       .append(alias).append(".").append(property4Buiding).append(" IN (SELECT RESOUCE_ID FROM SEC_USER_R_RIGTHS WHERE TYPE_CODE ='002' AND USER_ID = ").append(userId).append(" )")
//			       .append(endStr);
//		sql = begin+tmp+end;
		return proceed(buffer.toString(),associatedBuildingQuery);
	}
}
